package cn.stylefeng.guns.modular.storage.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.core.util.DateUtil;
import cn.stylefeng.guns.modular.basic.service.BasicCustomerService;
import cn.stylefeng.guns.modular.basic.service.BasicOperateService;
import cn.stylefeng.guns.modular.basic.service.BasicWarehouseService;
import cn.stylefeng.guns.modular.storage.entity.StoragePeriod;
import cn.stylefeng.guns.modular.storage.model.params.StorageOutDetailedParam;
import cn.stylefeng.guns.modular.storage.model.params.StoragePeriodParam;
import cn.stylefeng.guns.modular.storage.model.params.StoragePutSingleParam;
import cn.stylefeng.guns.modular.storage.model.params.StorageStockParam;
import cn.stylefeng.guns.modular.storage.model.result.StorageOutDetailedResult;
import cn.stylefeng.guns.modular.storage.model.result.StoragePeriodResult;
import cn.stylefeng.guns.modular.storage.model.result.StoragePutSingleResult;
import cn.stylefeng.guns.modular.storage.model.result.StorageStockResult;
import cn.stylefeng.guns.modular.storage.service.StorageOutDetailedService;
import cn.stylefeng.guns.modular.storage.service.StoragePeriodService;
import cn.stylefeng.guns.modular.storage.service.StoragePutSingleService;
import cn.stylefeng.guns.modular.storage.service.StorageStockService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;

/**
 * 期间库容统计控制器
 */
@Controller
@RequestMapping("/storagePeriod")
public class StoragePeriodController extends BaseController {

    private String PREFIX = "/storage/storagePeriod";

    @Autowired
    private StoragePeriodService storagePeriodService;

    @Autowired
    private UserService userService;

    @Autowired
    private BasicOperateService basicOperateService;

    @Autowired
    private BasicWarehouseService basicWarehouseService;

    @Autowired
    private BasicCustomerService basicCustomerService;

    /**
     * 跳转到主页面
     */
    @RequestMapping("")
    public String index(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	model.addAttribute("basicCustomerList", basicCustomerService.findAvailableList(deptId,deliverId,user.getOperateId()));
    	//查询可选择分公司
    	model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId,deliverId,user.getOperateId()));
    	//查询可选择仓库
    	model.addAttribute("basicWarehouseList", basicWarehouseService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/storagePeriod.html";
    }

    /**
     * 新增页面
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/storagePeriod_add.html";
    }

    /**
     * 编辑页面
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/storagePeriod_edit.html";
    }

    /**
     * 新增接口
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(StoragePeriodParam storagePeriodParam) {
        this.storagePeriodService.add(storagePeriodParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(StoragePeriodParam storagePeriodParam) {
        this.storagePeriodService.update(storagePeriodParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(StoragePeriodParam storagePeriodParam) {
        this.storagePeriodService.delete(storagePeriodParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(StoragePeriodParam storagePeriodParam) {
        StoragePeriod detail = this.storagePeriodService.getById(storagePeriodParam.getId());
        return ResponseData.success(detail);
    }

    @Autowired
    private StorageStockService storageStockService;

    @Autowired
    private StoragePutSingleService storagePutSingleService;

    @Autowired
    private StorageOutDetailedService storageOutDetailedService;
    
    /**
     * 查询列表
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(StoragePeriodParam storagePeriodParam,String condition ,String startTime,String endTime ) {
    	User user = userService.getcurrentUser();
    	storagePeriodParam.setDeptId(user.getDeptId());
    	if (ToolUtil.isNotEmpty(startTime) && ToolUtil.isNotEmpty(endTime)) {
    		storagePeriodParam.setDates("'"+startTime+"' and '"+endTime+"'");
		} else if (startTime == null && endTime == null){
			startTime = DateUtil.getDate();
			endTime = DateUtil.getDate();
			storagePeriodParam.setDates("'"+startTime+"' and '"+endTime+"'");
		}
    	LayuiPageInfo LayuiPageInfo = this.storagePeriodService.findPageBySpec(storagePeriodParam,condition,user.getOperateId());
    	List<StoragePeriodResult> list = LayuiPageInfo.getData();
    	if(list.size() > 0) {
    		for (StoragePeriodResult r : list) {
				if(r.getDates().equals(DateUtil.getDate())) {
					StorageStockParam param = new StorageStockParam();
					param.setWarehouseId(r.getWarehouseId());
					param.setCustomerId(r.getCustomerId());
					List<StorageStockResult> list2 = storageStockService.findListSumBySpec(param);//查询当日库存合计
					
					StorageStockParam param2 = new StorageStockParam();
					param2.setWarehouseId(r.getWarehouseId());
					param2.setCustomerId(r.getCustomerId());
					StorageStockResult kwshu = storageStockService.findKwShuBySpec(param2);//查询 已使用库位数
					if(kwshu != null) {
						r.setKwshu(kwshu.getKwshu());
					} else {
						r.setKwshu("0");
					}
					
					StoragePutSingleParam putSingleParam = new StoragePutSingleParam();
					putSingleParam.setRecommend(r.getDates());
					StoragePutSingleResult singleResult = storagePutSingleService.sumPutByDay(putSingleParam,r.getCustomerId(),r.getWarehouseId());//入库
					if(singleResult != null) {
						r.setPutNum(singleResult.getRealNum());
						r.setPutVolume(singleResult.getDetailedPutvolume());
						r.setPutWeight(singleResult.getPutWeight());
						r.setPutMoney(singleResult.getSingleMoney());
					} else {
						r.setPutNum("0");
						r.setPutVolume("0");
						r.setPutWeight("0");
						r.setPutMoney("0");
					}
					
					StorageOutDetailedParam outDetailedParam = new StorageOutDetailedParam();
					outDetailedParam.setRecommendBatchs(r.getDates());
					StorageOutDetailedResult outDetailedResult = storageOutDetailedService.sumOutByDay(outDetailedParam,r.getCustomerId(),r.getWarehouseId());//出库
					if(outDetailedResult != null) {
						r.setOutNum(outDetailedResult.getRealNum());
						r.setOutVolume(outDetailedResult.getStorageVolume());
						r.setOutWeight(outDetailedResult.getStorageWeight());
						r.setOutMoney(outDetailedResult.getStorageMoney());
					} else {
						r.setOutNum("0");
						r.setOutVolume("0");
						r.setOutWeight("0");
						r.setOutMoney("0");
					}

					if(list2.size()>0) {
						r.setQmNum(list2.get(0).getNum());
						r.setQmVolume(list2.get(0).getStockVolume());
						r.setQmWeight(list2.get(0).getStockWeight());
						r.setQmMoney(list2.get(0).getStockMoney());
					}
				}
			}
    	}
        return LayuiPageInfo;
    }

}


